﻿using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Utility;
using Utility.Helper;

namespace BLL
{
    public class Sys_DepartmentBLL : BaseBLL
    {
        public DataTable GetDataAllInfo(string whereStr)
        {
            string sql = string.Format(@"SELECT * FROM Sys_Department d WHERE 1=1");
            if (!string.IsNullOrEmpty(whereStr))
            {
                sql += whereStr;
            }
            sql += " ORDER BY d.DEPTID";
            DataTable dt = Query(sql);
            return dt;
        }

        public DataTable GetInfoByCmyID(string cmyId, bool istrue)
        {
            //获取所有部门信息
            var d_data = GetAll();
            //获取所有公司信息
            var c_data = new Sys_CompanyBLL().GetAll();
            //获取所有用户信息
            var user = new Sys_UserBLL();
            //根据公司ID 获取部门信息
            List<Sys_Department> dt_1;
            if (istrue)
            {
                dt_1 = d_data.Where(a => a.CMY_ID == decimal.Parse(cmyId)).OrderBy(a => a.DEPTID).ToList();
            }
            else
            {
                dt_1 = d_data.Where(a => a.DEPTID == decimal.Parse(cmyId)).OrderBy(a => a.DEPTID).ToList();
            }
            //组装数据

            var dt = new DataTable();
            dt.Columns.Add("DEPTID");
            dt.Columns.Add("CMY_ID");
            dt.Columns.Add("DEPTPARENTID");
            dt.Columns.Add("DEPTNAME");
            dt.Columns.Add("DEPTNUMBER");
            dt.Columns.Add("deptPName");
            dt.Columns.Add("CMY_NAME");
            dt.Columns.Add("DEPT_PERSONID");
            dt.Columns.Add("USER_NAME");
            dt.Columns.Add("DEPT_DEPERSONID");
            dt.Columns.Add("USER_NAMEO");
            dt.Columns.Add("DEPT_JXPERSONID");
            dt.Columns.Add("USER_NAMET");

            foreach (var item in dt_1)
            {
                var cmyname = "";
                {
                    var ex = c_data.SingleOrDefault(a => a.CMY_ID == item.CMY_ID);
                    if (ex != null)
                    {
                        cmyname = ex.CMY_NAME;
                    }
                }
                var deptname = "";
                {
                    //if (item.DEPTPARENTID == 0)
                    //{
                    var ex = d_data.SingleOrDefault(a => a.DEPTID == item.DEPTPARENTID);
                    if (ex != null)
                    {
                        deptname = ex.DEPTNAME;
                    }
                    //}
                }
                var pername = "";
                {
                    var ex = user.GetUsers(item.DEPT_PERSONID.ToString()).SingleOrDefault(a => a.USER_ID == item.DEPT_PERSONID);
                    if (ex != null)
                    {
                        pername = ex.USER_NAME;
                    }
                }
                var depername = "";
                {
                    var ex = user.GetUsers(item.DEPT_DEPERSONID.ToString()).SingleOrDefault(a => a.USER_ID == item.DEPT_DEPERSONID);
                    if (ex != null)
                    {
                        depername = ex.USER_NAME;
                    }
                }
                var jxpername = "";
                {
                    var ex = user.GetUsers(item.DEPT_JXPERSONID.ToString()).SingleOrDefault(a => a.USER_ID == item.DEPT_JXPERSONID);
                    if (ex != null)
                    {
                        jxpername = ex.USER_NAME;
                    }
                }
                var row = dt.NewRow();
                row["DEPTID"] = item.DEPTID;
                row["DEPTNAME"] = item.DEPTNAME;
                row["CMY_ID"] = item.CMY_ID;
                row["DEPTNUMBER"] = item.DEPTNUMBER;
                row["CMY_NAME"] = cmyname;
                row["DEPTPARENTID"] = item.DEPTPARENTID;
                row["deptPName"] = deptname;
                row["DEPT_PERSONID"] = item.DEPT_PERSONID;
                row["USER_NAME"] = pername;
                row["DEPT_DEPERSONID"] = item.DEPT_DEPERSONID;
                row["USER_NAMEO"] = depername;
                row["DEPT_JXPERSONID"] = item.DEPT_JXPERSONID;
                row["USER_NAMET"] = jxpername;
                dt.Rows.Add(row);
            }
            return dt;
        }

        //获取所有部门ID
        public DataTable GetInfoALLCmyID(string cmyId)
        {
            var sql = "SELECT DEPTID,CMY_ID,DEPTPARENTID,DEPTNAME,DEPTNUMBER FROM SYS_DEPARTMENT WHERE CMY_ID=" + cmyId;
            return Query(sql);
        }

        //根据ID递归查询部门信息
        public DataTable GetInfoDepID(string depId)
        {
            var sql = @"SELECT DEPTID, CMY_ID, DEPTPARENTID, DEPTNAME, DEPTNUMBER
              FROM SYS_DEPARTMENT
             WHERE DEPTID IN (SELECT DEPTID
                                FROM SYS_DEPARTMENT
                               START WITH DEPTID = " + depId + @"
                              CONNECT BY PRIOR DEPTID = DEPTPARENTID)";
            return Query(sql);
        }

        private List<Sys_Department> GetAll(int flag = 0)
        {
            var obj = HttpRuntimeCache.Get("Sys_Department");
            List<Sys_Department> data;
            if (obj == null || flag != 1)
            {
                //var sql = "SELECT DEPTID,CMY_ID,DEPTPARENTID,DEPTNAME,DEPTNUMBER FROM SYS_DEPARTMENT";
                var sql = "select deptid, cmy_id, deptparentid, deptname, deptnumber, dept_personid, dept_depersonid, state, dlevel, dept_jxpersonid from sys_department";
                var dt = Query(sql);
                data = new List<Sys_Department>();
                foreach (DataRow item in dt.Rows)
                {
                    var entity = new Sys_Department
                    {
                        DEPTID = Convert.ToDecimal(item["DEPTID"]),
                        CMY_ID = item["CMY_ID"] == DBNull.Value ? 0 : Convert.ToDecimal(item["CMY_ID"]),
                        DEPTPARENTID = item["DEPTPARENTID"] == DBNull.Value ? 0 : Convert.ToDecimal(item["DEPTPARENTID"]),
                        DEPTNAME = Convert.ToString(item["DEPTNAME"]),
                        DEPTNUMBER = item["DEPTNUMBER"] == DBNull.Value ? 0 : Convert.ToDecimal(item["DEPTNUMBER"]),
                        DEPT_PERSONID = item["DEPT_PERSONID"] == DBNull.Value ? 0 : Convert.ToDecimal(item["DEPT_PERSONID"]),
                        DEPT_DEPERSONID = item["DEPT_DEPERSONID"] == DBNull.Value ? 0 : Convert.ToDecimal(item["DEPT_DEPERSONID"]),
                        DEPT_JXPERSONID = item["DEPT_JXPERSONID"] == DBNull.Value ? 0 : Convert.ToDecimal(item["DEPT_JXPERSONID"]),
                        STATE = item["STATE"] == DBNull.Value ? 0 : Convert.ToInt32(item["STATE"]),
                        DLEVEL = item["DLEVEL"] == DBNull.Value ? 0 : Convert.ToInt32((item["DLEVEL"]))
                    };
                    data.Add(entity);
                }
                HttpRuntimeCache.Set("Sys_Department", data, 60, false);
            }
            else
            {
                data = (List<Sys_Department>)obj;
            }
            return data;
        }

        /// <summary>
        /// 根据部门ID查询子部门
        /// </summary>
        /// <param name="pdepid"></param>
        /// <returns></returns>
        public DataTable GetInfoByParentDeptID(string pdepid)
        {
            //查询所有部门
            var sql = "SELECT DEPTID,CMY_ID,DEPTPARENTID,DEPTNAME,DEPTNUMBER FROM THKQS.SYS_DEPARTMENT";
            var dt = Query(sql);
            var entitys = new List<Sys_Department>();
            foreach (DataRow item in dt.Rows)
            {
                var entity = new Sys_Department
                {
                    DEPTID = Convert.ToDecimal(item["DEPTID"]),
                    CMY_ID = item["CMY_ID"] == DBNull.Value ? 0 : Convert.ToDecimal(item["CMY_ID"]),
                    DEPTPARENTID = item["DEPTPARENTID"] == DBNull.Value ? 0 : Convert.ToDecimal(item["DEPTPARENTID"]),
                    DEPTNAME = Convert.ToString(item["DEPTNAME"]),
                    DEPTNUMBER = item["DEPTNUMBER"] == DBNull.Value ? 0 : Convert.ToDecimal(item["DEPTNUMBER"])
                };
                entitys.Add(entity);
            }

            var xx = GetInfoByParentDeptID(entitys, decimal.Parse(pdepid));
            var dtx = new DataTable();
            dtx.Columns.Add("DEPTID");
            dtx.Columns.Add("CMY_ID");
            dtx.Columns.Add("DEPTPARENTID");
            dtx.Columns.Add("DEPTNAME");
            dtx.Columns.Add("DEPTNUMBER");
            dtx.Columns.Add("deptPName");
            dtx.Columns.Add("CMY_NAME");
            dtx.Columns.Add("DEPT_PERSONID");
            dtx.Columns.Add("USER_NAME");
            dtx.Columns.Add("DEPT_DEPERSONID");
            dtx.Columns.Add("USER_NAMEO");
            dtx.Columns.Add("DEPT_JXPERSONID");
            dtx.Columns.Add("USER_NAMET");
            sql = "SELECT CMY_ID,CMY_NAME,CMY_NAME2,CMY_LEVEL,CMY_TYPE,CMY_PARENTID FROM SYS_COMPANY";
            dt = Query(sql);
            var entitysX = new List<Sys_Company>();
            foreach (DataRow item in dt.Rows)
            {
                var entity = new Sys_Company
                {
                    CMY_ID = decimal.Parse(dt.Rows[0]["CMY_ID"].ToString()),
                    CMY_NAME = dt.Rows[0]["CMY_NAME"].ToString(),
                    CMY_NAME2 = dt.Rows[0]["CMY_NAME2"] == DBNull.Value ? "" : dt.Rows[0]["CMY_NAME2"].ToString(),
                    CMY_LEVEL = decimal.Parse(dt.Rows[0]["CMY_LEVEL"].ToString()),
                    CMY_TYPE = dt.Rows[0]["CMY_TYPE"] == DBNull.Value ? "" : dt.Rows[0]["CMY_TYPE"].ToString(),
                    CMY_PARENTID = dt.Rows[0]["CMY_PARENTID"] == DBNull.Value ? 0 : decimal.Parse(dt.Rows[0]["CMY_PARENTID"].ToString())
                };
                entitysX.Add(entity);
            }

            foreach (var item in xx)
            {
                var row = dtx.NewRow();
                row["DEPTID"] = item.DEPTID;
                row["CMY_ID"] = item.CMY_ID;
                row["DEPTPARENTID"] = item.DEPTPARENTID;
                row["DEPTNAME"] = item.DEPTNAME;
                row["DEPTNUMBER"] = item.DEPTNUMBER;
                if (item.DEPTPARENTID == -1)
                {
                    row["deptPName"] = "";
                }
                else
                {
                    //var iteme = entitys.SingleOrDefault(a => a.CMY_ID == item.DEPTPARENTID);
                    var iteme = entitys.FirstOrDefault(a => a.CMY_ID == item.DEPTPARENTID);
                    row["deptPName"] = iteme == null ? "" : iteme.DEPTNAME;
                }
                //var itemx = entitysX.SingleOrDefault(a => a.CMY_ID == item.CMY_ID);
                var itemx = entitysX.FirstOrDefault(a => a.CMY_ID == item.CMY_ID);
                row["CMY_NAME"] = itemx == null ? "" : itemx.CMY_NAME;
                dtx.Rows.Add(row);
            }
            return dtx;
        }

        /// <summary>
        /// 根据部门ID递归查询子部门
        /// </summary>
        /// <param name="data"></param>
        /// <param name="pdepid"></param>
        /// <returns></returns>
        public List<Sys_Department> GetInfoByParentDeptID(List<Sys_Department> data, decimal pdepid)
        {
            var _data = data.Where(a => a.DEPTPARENTID == pdepid).ToList();
            var __data = new List<Sys_Department>();
            foreach (var item in _data)
            {
                __data.AddRange(GetInfoByParentDeptID(data, item.DEPTID));
            }
            _data.AddRange(__data);
            return _data;
        }

        public Sys_Department GetInfoById(string depid)
        {
            //string sql = "SELECT DEPTID,T1.CMY_ID,DEPTPARENTID AS DEPT_PARENT_ID,DEPTNAME AS DEPT_NAME,DEPTNUMBER AS DEPT_NUMBER FROM THKQS.SYS_DEPARTMENT T1 INNER JOIN THKQS.SYS_COMPANY T2 ON T1.CMY_ID = T2.CMY_ID WHERE DEPTID=" + depid;
            string sql = @"SELECT DEPTID,
        T1.CMY_ID,
        DEPTPARENTID    AS DEPT_PARENT_ID,
        DEPTNAME        AS DEPT_NAME,
        DEPTNUMBER      AS DEPT_NUMBER,
        DEPT_PERSONID,
        DEPT_DEPERSONID,
        DEPT_JXPERSONID,
        STATE,
        DLEVEL
   FROM SYS_DEPARTMENT T1
  INNER JOIN SYS_COMPANY T2
     ON T1.CMY_ID = T2.CMY_ID
  WHERE DEPTID = " + depid;
            DataTable dt = Query(sql);
            Sys_Department model = new Sys_Department();

            if (dt != null && dt.Rows.Count > 0)
            {
                model.DEPTID = decimal.Parse(dt.Rows[0]["DEPTID"].ToString());
                model.CMY_ID = decimal.Parse(dt.Rows[0]["CMY_ID"].ToString());
                model.DEPTPARENTID = decimal.Parse(dt.Rows[0]["DEPT_PARENT_ID"].ToString());
                model.DEPTNAME = dt.Rows[0]["DEPT_NAME"].ToString();
                model.DEPTNUMBER = decimal.Parse(dt.Rows[0]["DEPT_NUMBER"].ToString());

                model.DEPT_PERSONID = decimal.Parse(dt.Rows[0]["DEPT_PERSONID"].ToString() == "" ? "-1" : dt.Rows[0]["DEPT_PERSONID"].ToString());
                model.DEPT_DEPERSONID = decimal.Parse(dt.Rows[0]["DEPT_DEPERSONID"].ToString() == "" ? "-1" : dt.Rows[0]["DEPT_DEPERSONID"].ToString());
                model.DEPT_JXPERSONID = decimal.Parse(dt.Rows[0]["DEPT_JXPERSONID"].ToString() == "" ? "-1" : dt.Rows[0]["DEPT_JXPERSONID"].ToString());
                model.STATE = int.Parse(dt.Rows[0]["STATE"].ToString() == "" ? "-1" : dt.Rows[0]["STATE"].ToString());
                model.DLEVEL = int.Parse(dt.Rows[0]["DLEVEL"].ToString() == "" ? "-1" : dt.Rows[0]["DLEVEL"].ToString());
            }
            return model;
        }

        public int INSERT(Sys_Department model, string operName)
        {
            model.DEPTID = GetSequence("DEPTID");

            string sql = string.Format(@" insert into sys_department
   (deptid,
    cmy_id,
    deptparentid,
    deptname,
    deptnumber,
    dept_personid,
    dept_depersonid,
    state,
    dlevel,dept_jxpersonid)
 values ({0},{1},{2},'{3}',{4},{5},{6},{7},{8},{9})",
 model.DEPTID, model.CMY_ID, model.DEPTPARENTID, model.DEPTNAME, model.DEPTNUMBER, model.DEPT_PERSONID, model.DEPT_DEPERSONID, model.STATE, model.DLEVEL, model.DEPT_JXPERSONID);
            //            //弃用
            //            string sql = string.Format(@"INSERT INTO SYS_DEPARTMENT(
            //DEPTID,
            //CMY_ID,
            //DEPTPARENTID,
            //DEPTNAME,
            //DEPTNUMBER)
            //VALUES({0},{1},{2},'{3}',{4})
            //", model.DEPTID,model.CMY_ID, model.DEPTPARENTID, model.DEPTNAME, model.DEPTNUMBER);
            int count = NoQuery(sql, operName);
            return count;
        }

        public int UPDATE(Sys_Department model, string operName)
        {
            string sql = string.Format(@"update sys_department
   set cmy_id = {0},
       deptparentid = {1},
       deptname = '{2}',
       deptnumber = {3},
       dept_personid = {4},
       dept_depersonid = {5},
       state = {6},
       dlevel = {7},dept_jxpersonid={8}
 where deptid = {9}", model.CMY_ID, model.DEPTPARENTID, model.DEPTNAME, model.DEPTNUMBER, model.DEPT_PERSONID, model.DEPT_DEPERSONID, model.STATE, model.DLEVEL, model.DEPT_JXPERSONID, model.DEPTID);
            //            string sql = string.Format(@"UPDATE SYS_DEPARTMENT SET
            //            CMY_ID = '{1}',
            //            DEPTPARENTID = '{2}',
            //            DEPTNAME = '{3}',
            //            DEPTNUMBER = '{4}'
            //            WHERE DEPTID = '{0}'
            //", model.DEPTID, model.CMY_ID, model.DEPTPARENTID, model.DEPTNAME, model.DEPTNUMBER);

            int count = NoQuery(sql, operName);
            return count;
        }

        public int DELETE(string ids, string operName)
        {
            if (string.IsNullOrEmpty(ids))
            {
                return 0;
            }
            string sql = string.Format(@"DELETE FROM SYS_DEPARTMENT WHERE DEPTID IN(" + ids + ")");
            int count = NoQuery(sql, operName);
            return count;
        }

        public DataTable DeptSelect(string sqlWhere, string depid)
        {
            string sql = string.Format(@"SELECT
	sysd.DEPTID,
	sysd.CMY_ID,
	sysd.DEPTPARENTID,
	sysd.DEPTNAME,
	sysd.DEPTNUMBER,
	sd.DEPTNAME deptPName,
	sysc.CMY_NAME,
	sysd.DEPT_PERSONID,
	sysd.DEPT_DEPERSONID,
	sysd.DEPT_JXPERSONID
FROM
	THKQS.Sys_Department sysd,
	THKQS.Sys_Company sysc,
	THKQS.Sys_Department sd
WHERE
    sysd.CMY_ID = sysc.CMY_ID
    AND sysd.STATE=1
    AND sd.DEPTID = sysd.DEPTPARENTID
    AND sysd.DEPTID = '" + depid + @"'
    OR (sysd.DEPTID = sysd.DEPTPARENTID) " + sqlWhere + " ");
            DataTable dt = Query(sql);
            return dt;
        }

        public DataTable PositionSelect(string depid)
        {
            string sql = string.Format(@"SELECT
	            jxp.N_positionId,
	            jxp.C_positionName,
	            sysd.DEPTID,
	            sysd.DEPTNAME
            FROM
	            Jx_Position jxp,
	            THKQS.Sys_Department sysd
            WHERE
	            jxp.N_Dep_id = sysd.DEPTID");
            if (!(string.IsNullOrEmpty(depid)))
            {
                sql += @" AND jxp.N_Dep_id=" + depid;
            }
            DataTable dt = Query(sql);
            return dt;
        }

        public string GetDepNames(string whereStr)
        {
            string names = "";
            if (!string.IsNullOrEmpty(whereStr))
            {
                string sql = @"
SELECT
	d.DEPTNAME
FROM
	THKQS.Sys_Department d
WHERE
	DEPTID IN (" + whereStr + ")";
                DataTable dt = Query(sql);

                if (dt != null && dt.Rows.Count > 0)
                {
                    foreach (DataRow item in dt.Rows)
                    {
                        names += item[0].ToString() + ",";
                    }
                }

                if (names.Length > 0)
                {
                    names = names.Substring(0, names.LastIndexOf(','));
                }
            }
            return names;
        }

        public Sys_Department GetInfoByName(string dName)
        {
            string sql = string.Format(@"SELECT
    sysd.DEPTID,
    sysd.CMY_ID,
    sysd.DEPTPARENTID,
    sysd.DEPTNAME,
    sysd.DEPTNUMBER,
    (SELECT sd.DEPTNAME
    FROM THKQS.Sys_Department sd
    WHERE sd.DEPTID = sysd.DEPTPARENTID) deptPName,
    sysc.CMY_NAME,
    sysd.DEPT_PERSONID,
    sysd.DEPT_DEPERSONID,
    sysd.DEPT_JXPERSONID
FROM
    THKQS.Sys_Department sysd,
    THKQS.Sys_Company sysc
WHERE
    sysd.CMY_ID = sysc.CMY_ID
    AND sysd.DEPTNAME='" + dName + "'");

            DataTable dt = Query(sql);
            Sys_Department model = new Sys_Department();

            if (dt != null && dt.Rows.Count > 0)
            {
                model.CMY_ID = decimal.Parse(dt.Rows[0]["CMY_ID"].ToString());
                model.DEPTID = decimal.Parse(dt.Rows[0]["DEPTID"].ToString());
                model.DEPTNAME = dt.Rows[0]["DEPTNAME"].ToString();
                model.DEPTNUMBER = decimal.Parse(dt.Rows[0]["DEPTNUMBER"].ToString());
                model.DEPTPARENTID = decimal.Parse(dt.Rows[0]["DEPTPARENTID"].ToString());
                model.DEPT_PERSONID = decimal.Parse(dt.Rows[0]["DEPT_PERSONID"].ToString() == "" ? "-1" : dt.Rows[0]["DEPT_PERSONID"].ToString());
                model.DEPT_DEPERSONID = decimal.Parse(dt.Rows[0]["DEPT_DEPERSONID"].ToString() == "" ? "-1" : dt.Rows[0]["DEPT_DEPERSONID"].ToString());
                model.DEPT_JXPERSONID = decimal.Parse(dt.Rows[0]["DEPT_JXPERSONID"].ToString() == "" ? "-1" : dt.Rows[0]["DEPT_JXPERSONID"].ToString());
            }
            else
            {
                model = null;
            }
            return model;
        }

        public DataTable getDepartMentAndUser(string whereStr)
        {
            string sql = @"SELECT * FROM THKQS.Sys_Department d,THKQS.Sys_User u WHERE  u.USER_DEPARTMENT=d.DEPTID ";
            if (!string.IsNullOrEmpty(whereStr) && !string.IsNullOrEmpty(whereStr))
            {
                sql += whereStr;
            }
            return Query(sql);
        }

        public DataTable GetKHRY(string depid)
        {
            Qs_Base_Set baModel = new Qs_Base_SetBLL().GetModelByName("jx_rolid");//普通考核人员
            string sql = @"SELECT u.USER_ID,u.USER_NAME FROM THKQS.Sys_User u WHERE u.USER_DEPARTMENT=" + depid + " AND (','+CONVERT(nvarchar(20) , u.USER_ROLEID)+',') like '%," + baModel.Set_value + ",%' ORDER BY u.USER_ID,u.USER_NAME";
            return Query(sql);
        }

        public DataTable GetZHRY(string depid)
        {
            Qs_Base_Set gaModel = new Qs_Base_SetBLL().GetModelByName("jx_glrolid");//绩效综合岗
            string sql = @"SELECT u.USER_ID,u.USER_NAME FROM THKQS.Sys_User u WHERE u.USER_DEPARTMENT=" + depid + " AND (','+CONVERT(nvarchar(20) , u.USER_ROLEID)+',') like '%," + gaModel.Set_value + ",%' ORDER BY u.USER_ID,u.USER_NAME";
            return Query(sql);
        }

        public string GetBmldIds(string depid)
        {
            string ids = "";
            Qs_Base_Set daModel = new Qs_Base_SetBLL().GetModelByName("N_BMLDID");//绩效综合岗
            string sql = @"SELECT u.USER_ID,u.USER_NAME FROM THKQS.Sys_User u WHERE u.USER_DEPARTMENT=" + depid + " AND ((','+CONVERT(nvarchar(20) , u.USER_ROLEID)+',') like '%," + daModel.Set_value + ",%'" + " OR (','+CONVERT(nvarchar(20) , u.USER_ROLEID)+',') like '%," + 146 + ",%')";
            DataTable dt = Query(sql);

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (i == dt.Rows.Count - 1)
                {
                    ids += dt.Rows[i]["USER_ID"].ToString();
                }
                else
                {
                    ids += dt.Rows[i]["USER_ID"].ToString() + ",";
                }
            }

            if (dt.Rows.Count == 0)
            {
                Sys_Department depModel = new Sys_DepartmentBLL().GetInfoById(depid);
                ids = "";
                if (depModel.DEPT_DEPERSONID != 0)
                {
                    ids = depModel.DEPT_DEPERSONID.ToString();
                }
                if (depModel.DEPT_PERSONID != 0 && !string.IsNullOrEmpty(ids))
                {
                    ids = ids + "," + depModel.DEPT_PERSONID.ToString();
                }
                else if (depModel.DEPT_PERSONID != 0 && ids == "")
                {
                    ids = depModel.DEPT_PERSONID.ToString();
                }
            }

            return ids;
        }

        public DataTable GetBmldDt(string depid)
        {
            Qs_Base_Set daModel = new Qs_Base_SetBLL().GetModelByName("N_BMLDID");//绩效综合岗
            string sql = @"SELECT u.USER_ID,u.USER_NAME FROM THKQS.Sys_User u WHERE u.USER_DEPARTMENT=" + depid + " AND ((','||u.USER_ROLEID||',') like '%," + daModel.Set_value + ",%'" + " OR (','||u.USER_ROLEID||',') like '%," + 146 + ",%')";
            DataTable dt = Query(sql);

            if (dt.Rows.Count == 0)
            {
                Sys_Department depModel = new Sys_DepartmentBLL().GetInfoById(depid);
                string names = "";
                if (depModel.DEPT_DEPERSONID != 0)
                {
                    names = depModel.DEPT_DEPERSONID.ToString();
                }
                if (depModel.DEPT_PERSONID != 0 && !string.IsNullOrEmpty(names))
                {
                    names = names + "," + depModel.DEPT_PERSONID.ToString();
                }
                else if (depModel.DEPT_PERSONID != 0 && names == "")
                {
                    names = depModel.DEPT_PERSONID.ToString();
                }
                string[] idsl = names.Split(',');

                for (int i = 0; i < idsl.Length; i++)
                {
                    DataRow dr = dt.NewRow();
                    dr["USER_ID"] = idsl[i];
                    dr["USER_NAME"] = new Sys_UserBLL().GetUserNameS(idsl[i]).Trim();
                    dt.Rows.Add(dr);
                }
            }
            return dt;
        }

        /// <summary>
        /// 查询所有部门信息
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllInfo()
        {
            //查询sql
            string sql = string.Format(@"select sysd.deptid,
                                               sysd.cmy_id,
                                               sysd.deptparentid,
                                               sysd.deptname,
                                               sysd.deptnumber,
                                               (select sd.deptname
                                                  from sys_department sd
                                                 where sd.deptid = sysd.deptparentid) deptPName,
                                               sysc.cmy_name
                                          from sys_department sysd, sys_company sysc
                                         where sysd.cmy_id = sysc.cmy_id");

            sql += " order by sysd.deptid";
            //从数据库获取数据
            DataTable dt = Query(sql);
            //返回查询结果
            return dt;
        }

        public string GetDepIdByCompanyId(int companyId)
        {
            var sql = "SELECT T1.DEPTID FROM sys_department T1 WHERE  T1.Cmy_Id = " + companyId;
            var str = "";
            foreach (var item in Query(sql).AsEnumerable())
            {
                if (str != "")
                {
                    str += ",";
                }
                str += item.GetValue<string>("DEPTID");
            }
            return str;
        }
    }
}